const Base = require('../base.js');

module.exports = class extends Base {
  /**
   * @api {POST} /conferenceRoom 添加会议室
   * @apiName add_conferenceRoom
   * @apiGroup conferenceRoom
   * @apiParam  {String} addr 会议室地址
   * @apiParam  {String} name 会议室名称
   * @apiParam  {Number} peopleNum 容纳人数
   * @apiSuccess (201) {Object} res 新增的会议室对象
   * @apiVersion  1.0.0
   */
  async addAction() {
    const params = {
      addr: this.post('addr'),
      name: this.post('name'),
      peopleNum: this.post('peopleNum')
    };
    const res = await this.model('conference_room', 'mysqlLocal').add(params);
    if (res) {
      params.id = res;
      this.status = 201;
      this.success(params, 'add ok');
    } else {
      this.fail(-1, 'add fail:' + res);
    }
  }

  /**
   * @api {PUT} /conferenceRoom/:id 修改会议室
   * @apiName edit_conferenceRoom
   * @apiGroup conferenceRoom
   * @apiParam  {Number} id 会议室唯一id标识
   * @apiParam  {String} addr 会议室地址
   * @apiParam  {String} name 会议室名称
   * @apiParam  {Number} peopleNum 容纳人数
   * @apiSuccess (201) {Object} res 修改后的完成对象
   * @apiVersion  1.0.0
   */
  async editAction() {
    const params = {
      addr: this.post('addr'),
      name: this.post('name'),
      peopleNum: this.post('peopleNum')
    };
    const res = await this.model('conference_room', 'mysqlLocal').where({id: this.get('id')}).update(params);
    params.id = this.get('id');
    if (res) {
      this.status = 201;
      this.success(params, 'update ok');
    } else {
      this.fail(-1, 'update fail:' + res);
    }
  }

  /**
   * @api {DELETE} /conferenceRoom/:id 删除会议室
   * @apiName delete_conferenceRoom
   * @apiGroup conferenceRoom
   * @apiParam  {Number} id 会议室唯一id标识
   * @apiSuccess (204) {String} null 删除成功返回空文档
   * @apiVersion  1.0.0
   */
  async delAction() {
    const res = await this.model('conference_room', 'mysqlLocal').where({id: this.get('id')}).delete();
    if (res) {
      this.status = 204;
      this.success(res, 'delete ok');
    } else {
      this.fail(-1, 'delete fail:' + res);
    }
  }

  /**
   * @api {GET} /conferenceRoom 获取会议室列表
   * @apiName get_conferenceRoom
   * @apiGroup conferenceRoom
   * @apiSuccess (200) {Object[]} res 会议室列表
   * @apiVersion  1.0.0
   */
  async listAction() {
    const res = await this.model('conference_room', 'mysqlLocal').select();
    if (res) {
      this.success(res, 'list ok');
    } else {
      this.fail(-1, 'list fail:' + res);
    }
  }
};
